<!--
 * @Description: 销量趋势图表
 * @Author: 梁萌
 * @Date: 2020-11-29 17:49:47
 * @LastEditTime: 2020-12-19 16:53:54
 * @FilePath: \vision\src\components\Trend.vue
-->

<template>
  <div class="com-container">
    <!-- 下拉列表 -->
    <div class="title" :style="titleStyle">
      <span @click="showChoice=!showChoice">{{'▎ '+this.titleContext}}</span>
      <span class="iconfont title-icon" :style="titleStyle" @click="showChoice=!showChoice">&#xe6eb;</span>
      <div class="select-con" v-show="showChoice" :style="selectStyle">
        <div
          class="select-item"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handSelect(item.key)"
        >{{item.text}}</div>
      </div>
    </div>
    <!-- 图表 -->
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      showChoice: false, //是否显示下拉菜单
      choiceType: "map", //显示数据的类型
      titleFontSize: 0, //标题的字体大小
    };
  },
  created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("trendData", this.getData);
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "trendData",
      chartName: "trend",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("trendData");
  },
  computed: {
    //标题下拉列表数据
    selectTypes() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type.filter((item) => {
          return item.key !== this.choiceType;
        });
      }
    },
    //标题显示内容
    titleContext() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    //设置标题的样式
    titleStyle() {
      return {
        fontSize: this.titleFontSize + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
    //设置下拉列表样式
    selectStyle() {
      return {
        marginLeft: this.titleFontSize - 3 + "px",
      };
    },
    ...mapState(["theme"]),
  },
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.trend_ref, this.theme); //初始化echarts实例对象
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "",
          left: 20,
          top: 20,
        },
        grid: {
          top: "35%",
          left: "3%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false, //数据紧挨y轴显示
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis", //鼠标放在柱状图条目上时显示文字信息
        },
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
      };
      this.chartIntance.setOption(initOption);
      //对图表对象进行数据事件监听
      this.chartIntance.on("mouseover", () => {
        clearInterval(this.timerId);
      });

      this.chartIntance.on("mouseout", () => {
        this.startInterval();
      });
    },

    //获取服务器数据
    //ret为服务器发送给客户端图表的数据
    getData(ret) {
      //const { data: ret } =await this.$http.get("trend");
      this.allData = ret;
      // console.log(this.allData);
      this.updateChart();
    },

    //更新图表
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];
      const timeArr = this.allData.common.month; //类目轴信息
      const yData = this.allData[this.choiceType].data; //y轴数据
      //折线数据
      const seriesArr = yData.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      //图例数据
      const legendData = yData.map((item) => {
        return item.name;
      });

      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        series: seriesArr,
        legend: {
          data: legendData,
        },
      };

      this.chartIntance.setOption(dataOption);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      //和分辨率大小相关配置
      const adapterOption = {
        //设置图例的大小
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize();
    },

    //下拉列表切换事件
    handSelect(selectType) {
      this.choiceType = selectType;
      this.updateChart(); //更新图表数据
      this.showChoice = false;
    },
  },
  watch: {
    theme() {
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
};
</script>


<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;

  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }

  .select-con {
    background-color: #222733;
  }
}
</style>